
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="utf-8">
    <title>Instrumenting Web Applications | Metrics</title>
    <link rel="stylesheet" href="../_static/pygments.css" type="text/css"/>
    <link rel="stylesheet" href="../_static/metrics.css" type="text/css"/>
    <link rel="top" title="Metrics" href="../index.html"/>
    <link rel="up" title="User Manual" href="index.html"/>
    <link rel="next" title="Third Party Libraries" href="third-party.html"/>
    <link rel="prev" title="Metrics Servlets" href="servlets.html"/>
    <style lang="text/css">
        #top-bar, #top-bar small, #top-bar a {
            text-shadow: 0px -1px 0px #cf2c0f;
            color: #fff;
        }
        
        #top-bar {
            background-color: #ED4A2D;
            background-image: -moz-linear-gradient(top, #ff684b, #cf2c0f);
            background-image: -ms-linear-gradient(top, #ff684b, #cf2c0f);
            background-image: -webkit-gradient(linear, 0 0, 0 100%, from(#ff684b), to(#cf2c0f));
            background-image: -webkit-linear-gradient(top, #ff684b, #cf2c0f);
            background-image: -o-linear-gradient(top, #ff684b, #cf2c0f);
            background-image: linear-gradient(top, #ff684b, #cf2c0f);
            filter: progid:DXImageTransform.Microsoft.gradient(startColorstr = '#ff684b', endColorstr = '#cf2c0f', GradientType = 0);
            -webkit-border-radius: 0;
            -moz-border-radius: 0;
            border-radius: 0;
        }


        .hero-unit {
            background-image: url("../_static/metrics-hat.png") !important;
            background-repeat: no-repeat !important;
            background-position: 30px 50px;
        }

        .hero-unit div.section {
            padding-left: 200px !important;
        }
    </style>
</head>
<body>
    <a href="https://github.com/dropwizard/metrics">
        <img style="position: absolute; top: 0; right: 0; border: 0;"
             src=""
             alt="Fork me on GitHub"></a>
    <div class="navbar">
        <div class="navbar-inner container-fluid" id="top-bar">
            <header class="row-fluid">
                <h1 class="span12" id="title"><img class="logo" src="../_static/metrics-logo.png" alt="Logo"/>
                    <a href="../index.html">Metrics</a>
                    <small>Mind the gap.</small>
                </h1>
            </header>
        </div>
    </div>
    <div class="container-fluid">
        <div class="row-fluid">
            <div class="span3" id="sidebar">
                <ul class="current">
<li class="toctree-l1"><a class="reference internal" href="../getting-started.html">Getting Started</a></li>
<li class="toctree-l1 current"><a class="reference internal" href="index.html">User Manual</a><ul class="current">
<li class="toctree-l2"><a class="reference internal" href="core.html">Metrics Core</a></li>
<li class="toctree-l2"><a class="reference internal" href="healthchecks.html">Health Checks</a></li>
<li class="toctree-l2"><a class="reference internal" href="ehcache.html">Instrumenting Ehcache</a></li>
<li class="toctree-l2"><a class="reference internal" href="ganglia.html">Reporting to Ganglia</a></li>
<li class="toctree-l2"><a class="reference internal" href="graphite.html">Reporting to Graphite</a></li>
<li class="toctree-l2"><a class="reference internal" href="httpclient.html">Instrumenting Apache HttpClient</a></li>
<li class="toctree-l2"><a class="reference internal" href="jdbi.html">Instrumenting JDBI</a></li>
<li class="toctree-l2"><a class="reference internal" href="jersey.html">Instrumenting Jersey 1.x</a></li>
<li class="toctree-l2"><a class="reference internal" href="jersey.html#instrumenting-jersey-2-x">Instrumenting Jersey 2.x</a></li>
<li class="toctree-l2"><a class="reference internal" href="jetty.html">Instrumenting Jetty</a></li>
<li class="toctree-l2"><a class="reference internal" href="log4j.html">Instrumenting Log4j</a></li>
<li class="toctree-l2"><a class="reference internal" href="logback.html">Instrumenting Logback</a></li>
<li class="toctree-l2"><a class="reference internal" href="jvm.html">JVM Instrumentation</a></li>
<li class="toctree-l2"><a class="reference internal" href="json.html">JSON Support</a></li>
<li class="toctree-l2"><a class="reference internal" href="servlets.html">Metrics Servlets</a></li>
<li class="toctree-l2 current"><a class="current reference internal" href="#">Instrumenting Web Applications</a></li>
<li class="toctree-l2"><a class="reference internal" href="third-party.html">Third Party Libraries</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="../about/index.html">About Metrics</a></li>
</ul>

                <hr/>
                <ul>
                    <li><a href="https://dropwizard.github.io/metrics/3.2.0/apidocs/">Javadoc</a></li>
                    <li><a href="https://groups.google.com/forum/#!forum/metrics-user">Mailing List</a></li>
                </ul>
            </div>
            <div class="span9" id="body">
                
  <div class="section" id="instrumenting-web-applications">
<span id="manual-servlet"></span><h1>Instrumenting Web Applications</h1>
<p>The <code class="docutils literal"><span class="pre">metrics-servlet</span></code> module provides a Servlet filter which has meters for status codes, a
counter for the number of active requests, and a timer for request duration. By default the filter
will use <code class="docutils literal"><span class="pre">com.codahale.metrics.servlet.InstrumentedFilter</span></code> as the base name of the metrics.
You can use the filter in your <code class="docutils literal"><span class="pre">web.xml</span></code> like this:</p>
<div class="highlight-xml"><div class="highlight"><pre><span></span><span class="nt">&lt;filter&gt;</span>
    <span class="nt">&lt;filter-name&gt;</span>instrumentedFilter<span class="nt">&lt;/filter-name&gt;</span>
    <span class="nt">&lt;filter-class&gt;</span>com.codahale.metrics.servlet.InstrumentedFilter<span class="nt">&lt;/filter-class&gt;</span>
<span class="nt">&lt;/filter&gt;</span>
<span class="nt">&lt;filter-mapping&gt;</span>
    <span class="nt">&lt;filter-name&gt;</span>instrumentedFilter<span class="nt">&lt;/filter-name&gt;</span>
    <span class="nt">&lt;url-pattern&gt;</span>/*<span class="nt">&lt;/url-pattern&gt;</span>
<span class="nt">&lt;/filter-mapping&gt;</span>
</pre></div>
</div>
<p>An optional filter init-param <code class="docutils literal"><span class="pre">name-prefix</span></code> can be specified to override the base name
of the metrics associated with the filter mapping. This can be helpful if you need to instrument
multiple url patterns and give each a unique name.</p>
<div class="highlight-xml"><div class="highlight"><pre><span></span><span class="nt">&lt;filter&gt;</span>
    <span class="nt">&lt;filter-name&gt;</span>instrumentedFilter<span class="nt">&lt;/filter-name&gt;</span>
    <span class="nt">&lt;filter-class&gt;</span>com.codahale.metrics.servlet.InstrumentedFilter<span class="nt">&lt;/filter-class&gt;</span>
    <span class="nt">&lt;init-param&gt;</span>
        <span class="nt">&lt;param-name&gt;</span>name-prefix<span class="nt">&lt;/param-name&gt;</span>
        <span class="nt">&lt;param-value&gt;</span>authentication<span class="nt">&lt;/param-value&gt;</span>
    <span class="nt">&lt;/init-param&gt;</span>
<span class="nt">&lt;/filter&gt;</span>
<span class="nt">&lt;filter-mapping&gt;</span>
    <span class="nt">&lt;filter-name&gt;</span>instrumentedFilter<span class="nt">&lt;/filter-name&gt;</span>
    <span class="nt">&lt;url-pattern&gt;</span>/auth/*<span class="nt">&lt;/url-pattern&gt;</span>
<span class="nt">&lt;/filter-mapping&gt;</span>
</pre></div>
</div>
<p>You will need to add your <code class="docutils literal"><span class="pre">MetricRegistry</span></code> to the servlet context as an attribute named
<code class="docutils literal"><span class="pre">com.codahale.metrics.servlet.InstrumentedFilter.registry</span></code>. You can do this using the Servlet API
by extending <code class="docutils literal"><span class="pre">InstrumentedFilterContextListener</span></code>:</p>
<div class="highlight-java"><div class="highlight"><pre><span></span><span class="kd">public</span> <span class="kd">class</span> <span class="nc">MyInstrumentedFilterContextListener</span> <span class="kd">extends</span> <span class="n">InstrumentedFilterContextListener</span> <span class="o">{</span>
    <span class="kd">public</span> <span class="kd">static</span> <span class="kd">final</span> <span class="n">MetricRegistry</span> <span class="n">REGISTRY</span> <span class="o">=</span> <span class="k">new</span> <span class="n">MetricRegistry</span><span class="o">();</span>

    <span class="nd">@Override</span>
    <span class="kd">protected</span> <span class="n">MetricRegistry</span> <span class="nf">getMetricRegistry</span><span class="o">()</span> <span class="o">{</span>
        <span class="k">return</span> <span class="n">REGISTRY</span><span class="o">;</span>
    <span class="o">}</span>
<span class="o">}</span>
</pre></div>
</div>
</div>


            </div></div>
        <hr/>
        <footer>
            <p style="float: left">
            &copy; Copyright 2010-2014, Coda Hale, Yammer Inc..
            Created using <a href="http://sphinx.pocoo.org/">Sphinx</a>
            1.5.2.
            </p>
            <p style="float: right">Dropwizard Metrics v3.2.0</p>
        </footer>
    </div>
</body>
</html>


